package com.mickey.controller;

import com.mickey.access.AccessLimit;
import com.mickey.domain.MiaoshaUser;
import com.mickey.domain.User;
import com.mickey.rabbitmq.MQSender;
import com.mickey.redis.RedisService;
import com.mickey.redis.UserKey;
import com.mickey.result.CodeMsg;
import com.mickey.result.Result;
import com.mickey.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MQSender mqSender;


    @RequestMapping("/mq/topic")
    @ResponseBody
    public String mqtopci(){
        mqSender.sendTopic("hello word");
        return "hello Word!";
    }

    @RequestMapping("/mq/fanout")
    @ResponseBody
    public String fanout(){
        mqSender.sendFanout("hello word");
        return "hello Word!";
    }

    @RequestMapping("/mq/header")
    @ResponseBody
    public String mqheader(){
        mqSender.sendHeaders("hello word");
        return "hello Word!";
    }

    @RequestMapping("/mq")
    @ResponseBody
    public String mq(){
        mqSender.send("13132");
        return "hello Word!";
    }


    @AccessLimit(seconds = 3,maxCount = 3,needLogin = true)
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello Word!";
    }

    @AccessLimit(seconds = 3,maxCount = 3,needLogin = false)
    @RequestMapping("/suc")
    @ResponseBody
    public Result<String > sucess() {
        return Result.sucess("111");
    }

    @RequestMapping("/err")
    @ResponseBody
    public Result error(){
        return Result.err(CodeMsg.SERVICE_ERROR);
    }

    @RequestMapping("/thy")
    public String thy(Model model){
        model.addAttribute("name","Czx");
        return "hello";
    }

    @RequestMapping("/dbGet")
    @ResponseBody
    public Result<User> dbGet(){
        User user = userService.getUserById(1);
        return Result.sucess(user);
    }

    @RequestMapping("/tx")
    @ResponseBody
    public Result<User> dbTx(){
        User user = userService.tx();
        return Result.sucess(user);
    }


    @RequestMapping("/redisGet")
    @ResponseBody
    public Result redisGet(){
        User aa = redisService.get(UserKey.getById,"1",User.class);
        return Result.sucess(aa);
    }

    @RequestMapping("/redisSet")
    @ResponseBody
    public Result<Boolean> redisSet(){
        User user = new User();
        user.setId(1);
        user.setName("czx");
        redisService.set(UserKey.getById,""+1,user);
        return Result.sucess(true);
    }


    @RequestMapping("/info")
    @ResponseBody
    public Result<MiaoshaUser> info(MiaoshaUser user){
        mqSender.send("13132");
        return Result.sucess(user);
    }



}
